package org.google.openfrm.Networks;

import org.google.openfrm.nodes.*;
import org.google.openfrm.Networks.Node_Connection;

import java.util.ArrayList;
import java.util.HashMap;

public class SampleRiskEvaluationStructure extends NetworkStructure {
    static Simple_Node event1 = new Simple_Node(1);
    static Simple_Node event2 = new Simple_Node(2);
    static Simple_Node event3 = new Simple_Node(3);
    static Simple_Node event4 = new Simple_Node(4);
    static Utility_Node creditRiskEvaluator = new Utility_Node(5);
    static Decision_Node buyingDecision = new Decision_Node(6);
    static int totalNodeNum = 6;

    public SampleRiskEvaluationStructure() {
        addNodes();
        generateConnections();
        setProbabilities();
    }

    public static void generateConnections() {
        connections = new ArrayList<Node_Connection>();
        connections.add(new Node_Connection(1, 3));
        connections.add(new Node_Connection(2, 3));
        connections.add(new Node_Connection(3, 5));
        connections.add(new Node_Connection(4, 5));
        connections.add(new Node_Connection(5, 6));
        setWaysToNodes();
    }

    public static void setWaysToNodes() {
        ArrayList<ArrayList<Bayes_Node>> listsWaysOut = new ArrayList<ArrayList<Bayes_Node>>();
        for (int i = 0; i < totalNodeNum; i++) {
            ArrayList<Bayes_Node> list = new ArrayList<Bayes_Node>();
            listsWaysOut.add(list);
        }
        for (Node_Connection conn : connections) {
            System.out.println("Connection between "+conn.getFrom()+ " and "+conn.getTo());
            Bayes_Node from = nodes.get(conn.getFrom());
            Bayes_Node to = nodes.get(conn.getTo());
            from.addWay_out(to);
            to.addWay_in(from);
        }
    }

    public static void addNodes() {
        event1.setMarginal(true);
        event2.setMarginal(true);
        event3.setMarginal(false);
        event4.setMarginal(true);
        creditRiskEvaluator.setMarginal(false);
        buyingDecision.setMarginal(false);
        
        nodes = new HashMap<Integer, Bayes_Node>();
        nodes.put(event1.getId(), event1);
        nodes.put(event2.getId(), event2);
        nodes.put(event3.getId(), event3);
        nodes.put(event4.getId(), event4);
        nodes.put(creditRiskEvaluator.getId(), creditRiskEvaluator);
        nodes.put(buyingDecision.getId(), buyingDecision);

    }

    public static void setProbabilities() {
        event1.setMarginalProbability(0.1d);

        event2.setMarginalProbability(0.1d);

        ArrayList<Double> probEvent3 = new ArrayList<Double>();
        probEvent3.add(0.05d);
        probEvent3.add(0.01d);
        event3.setConditionalProbabilities(probEvent3);

        event4.setMarginalProbability(0.03d);

    }

   public Bayes_Node getNodeById(int id) {
        return nodes.get(id);
    }

}
